package com.superzhou.dragview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author Administrator
 */
public class MainActivity extends AppCompatActivity {

    private TextView allGold;
    private TextView state;
    private WindowManager wm;
    private LayoutInflater from;
    private WindowManager wm1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        from = LayoutInflater.from(this);
        final DragViewGroup group = findViewById(R.id.drag_view);
        allGold = findViewById(R.id.all_gold);
        state = findViewById(R.id.activity_state);
        group.setListener(new DragChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void OnChangeNumber(int num) {
                allGold.setText(num+Integer.parseInt(TextUtils.isEmpty(allGold.getText().toString())?"0": allGold.getText().toString())+"");
            }
        });
        TextView button = (TextView) getLayoutInflater().inflate(R.layout.bottom_button,group,false);
        group.addBottomButton(button);
        List<DataBean> dataArray = new ArrayList<>();
        dataArray.add(new DataBean(1,2500,0,"lv1"));
        dataArray.add(new DataBean(1,2500,1,"lv1"));
        dataArray.add(new DataBean(1,2500,2,"lv1"));
        dataArray.add(new DataBean(1,2500,3,"lv1"));
        dataArray.add(new DataBean(1,2500,4,"lv1"));
        dataArray.add(new DataBean(1,2500,5,"lv1"));
        dataArray.add(new DataBean(1,2500,6,"lv1"));
        dataArray.add(new DataBean(1,2500,7,"lv1"));
        dataArray.add(new DataBean(1,2500,8,"lv1"));
        dataArray.add(new DataBean(1,2500,9,"lv1"));
        dataArray.add(new DataBean(1,2500,10,"lv1"));
        dataArray.add(new DataBean(1,2500,11,"lv1"));
        group.setDataBeans(dataArray);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (group.canBePurchased()){
                        int index = group.getFirstPlacement();
                        System.out.println("index"+index);
                        if (index >= 0){
                            group.addItem(new DataBean(2,3500,index,"lv2"));
                        }else{
                            Toast.makeText(MainActivity.this,"坑位不足 无法购买!",Toast.LENGTH_LONG).show();
                        }
                    }else{
                        Toast.makeText(MainActivity.this,"坑位不足 无法购买!",Toast.LENGTH_LONG).show();
                    }
            }
        });
        ChatView chatView = new ChatView(this);
        chatView.show();
        chatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addViewtoWindow();
            }
        });
/*
        LayoutInflater inflater = getLayoutInflater();
        View mView = inflater.inflate(R.layout.child_item, group, false);
        ImageView viewById = mView.findViewById(R.id.image_view);
        viewById.setImageResource(R.mipmap.lv38_2_5cai);
        mView.setTag(2);
        group.addView(mView);
        View mView2 = inflater.inflate(R.layout.child_item, group, false);
        ImageView viewById2 = mView2.findViewById(R.id.image_view);
        viewById2.setImageResource(R.mipmap.lv1);
        mView2.setTag(11);
        group.addView(mView2);*/
    }

    @Override
    protected void onStart() {
        super.onStart();
        state.setText("生命周期方法 onStart 执行");
    }
    @Override
    protected void onPause() {
        super.onPause();
        state.setText("生命周期方法 onPause 执行");
    }
    @Override
    protected void onResume() {
        super.onResume();
        state.setText("生命周期方法 onResume 执行");
    }

    @Override
    protected void onStop() {
        super.onStop();

        state.setText("生命周期方法 onStop 执行");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        state.setText("生命周期方法 onDestroy 执行");
    }

    private void addViewtoWindow(){
      /*  wm1 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        final View view = from.inflate(R.layout.child_view, null);
        view.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wm1.removeView(view);
            }
        });
        //屏宽
       int screenWidth = wm1.getDefaultDisplay().getWidth();
        //屏高
       int screenHeight = wm1.getDefaultDisplay().getHeight();
        //布局设置
        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
        // 设置window type
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        *//*
        // 设置图片格式，效果为背景透明
         *//*
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        // 设置Window flag
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.width = screenWidth-200;
        wmParams.height = screenWidth-400;
        wmParams.y = 200;
        wm1.addView(view, wmParams);*/
        MyDragView dragView = new MyDragView(this);
        dragView.show();
    }


}